﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Operations;

namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandOperation : SerializedCommand
    {
        //public event ManipulatorEvent StateChanged;      

        private IOperation operation;
        public CommandOperation(ContourControl control, IOperation operation) : base(control)
        {
            this.operation = operation;            
            this.operation.OperationEvent += new OperationEvent(operation_OperationEvent);
        }

        void operation_OperationEvent(IOperation sender, OperationEventState operationState)
        {
            if (operationState == OperationEventState.EndExecute)
                this.Exit();
            else if (operationState == OperationEventState.Canceled)
                this.Cancel();
        }

        public CommandOperation(ContourControl control)
            : base(control)
        {
        }

        public override void Execute()
        {
            base.Execute();
            operation.Control = this.control;
            operation.Execute();
        }

        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);
            operation.ObtainPeripheryState(state);
        }

        public override void DrawOnGraphics(System.Drawing.Graphics g, int screenWidth, int screenHeight, System.Drawing.RectangleF modelRectangle, ITAP.Core.Modelling.ModelPoint cameraCenter, ITAP.Core.Calculations.ModelCoordinateConverter coordinateConverter)
        {
            operation.DrawOnGraphics(g, screenWidth, screenHeight, modelRectangle, cameraCenter, coordinateConverter);
        }

        public override bool Enabled
        {
            get
            {
                return operation.Enabled;
            }
            set
            {
                operation.Enabled = value;
            }
        }

        public override string Name
        {
            get
            {
                return operation.Name;
            }
        }

        public override bool CanUndo
        {
            get
            {
                return operation.CanUndo;
            }
        }

        public override object Clone()
        {
            CommandOperation clone = (CommandOperation)base.Clone();
            clone.operation = this.operation;
            clone.state = this.state;
            this.operation.OperationEvent -= this.operation_OperationEvent;
            clone.operation.OperationEvent += new OperationEvent(clone.operation_OperationEvent);
            return clone;
        }

        public override string ToString()
        {
            return operation.ToString();
        }
    }
}
